<template>
	<div class="system-user-container layout-padding">
		<el-row :gutter="8" style="width: 100%">
			<el-col :span="4" :xs="24">
				<OrgTree ref="orgTreeRef" @node-click="nodeClick" />
			</el-col>
      <el-col :span="20" :xs="24">
				<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
					<el-form ref="queryForm" :inline="true">
						<el-form-item label="条件：">
							<el-input v-model="state.userTableData.queryParams.search" placeholder="账号/姓名/手机号码" clearable />
						</el-form-item>
						<el-form-item>
							<el-button-group>
								<el-button type="primary" icon="ele-Search" @click="handleQuery"> 查询 </el-button>
								<el-button icon="ele-Refresh" @click="resetQuery"> 重置 </el-button>
							</el-button-group>
						</el-form-item>
						<el-form-item>
							<el-button type="primary" icon="ele-Plus" @click="openAddUser('add')" > 新增 </el-button>
						</el-form-item>
					</el-form>
				</el-card>

				<el-card class="full-table" shadow="hover" style="margin-top: 8px">
					<el-table :data="state.userTableData.data" style="width: 100%" v-loading="state.userTableData.loading" border>
						<el-table-column type="index" label="序号" width="70" align="center" fixed />
						<el-table-column prop="account" label="账号" width="120" align="center" fixed show-overflow-tooltip />
						<el-table-column prop="nickName" label="昵称" width="120" align="center" show-overflow-tooltip />
						<el-table-column prop="realName" label="姓名" width="120" align="center" show-overflow-tooltip />
						<el-table-column label="头像" width="80" align="center" show-overflow-tooltip>
							<template #default="scope">
								<el-avatar :src="scope.row.avatar" size="small">{{ scope.row.nickName?.slice(0, 1) ?? scope.row.realName?.slice(0, 1) }} </el-avatar>
							</template>
						</el-table-column>
						<el-table-column prop="phone" label="手机号码" width="120" align="center" show-overflow-tooltip />
						<el-table-column label="出生日期" width="100" align="center" show-overflow-tooltip>
							<template #default="scope">
								{{ formatDate(new Date(scope.row.birthday), 'YYYY-mm-dd') }}
							</template>
						</el-table-column>
						<el-table-column label="性别" width="70" align="center" show-overflow-tooltip>
							<template #default="scope">
								<el-tag type="success" v-if="scope.row.sex === 1"> 男 </el-tag>
								<el-tag type="danger" v-else> 女 </el-tag>
							</template>
						</el-table-column>
						<el-table-column label="账号类型" width="100" align="center" show-overflow-tooltip>
							<template #default="scope">
								<el-tag v-if="scope.row.accountType === 888"> 系统管理员 </el-tag>
								<el-tag v-else-if="scope.row.accountType === 777"> 普通账号 </el-tag>
								<el-tag v-else-if="scope.row.accountType === 666"> 会员 </el-tag>
								<el-tag v-else> 其他 </el-tag>
							</template>
						</el-table-column>
						<el-table-column label="状态" width="70" align="center" show-overflow-tooltip>
							<template #default="scope">
								<el-switch v-model="scope.row.status" :active-value="1" :inactive-value="2" size="small" @change="changeStatus(scope.row)"/>
							</template>
						</el-table-column>
						<!-- <el-table-column prop="orderNo" label="排序" width="70" align="center" show-overflow-tooltip /> -->
						<el-table-column prop="createTime" label="修改时间" width="160" align="center" show-overflow-tooltip />
						<el-table-column prop="remark" label="备注" header-align="center" show-overflow-tooltip />
						<el-table-column label="操作" width="110" align="center" fixed="right" show-overflow-tooltip>
							<template #default="scope">
								<el-button icon="ele-Edit" size="small" text type="primary" @click="openEditUser('edit',scope.row)" > 编辑 </el-button>
								<el-dropdown>
									<el-button icon="ele-MoreFilled" size="small" text type="primary" style="padding-left: 12px" />
									<template #dropdown>
										<el-dropdown-menu>
											<el-dropdown-item icon="ele-RefreshLeft" @click="resetUserPwd(scope.row)"> 重置密码 </el-dropdown-item>
											<el-dropdown-item icon="ele-Delete" @click="delUser(scope.row)" divided > 删除账号 </el-dropdown-item>
										</el-dropdown-menu>
									</template>
								</el-dropdown>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination
						v-model:currentPage="state.userTableData.queryParams.pageIndex"
						v-model:page-size="state.userTableData.queryParams.pageSize"
						:total="state.userTableData.total"
						:page-sizes="[10, 20, 50, 100]"
						small
						background
						@size-change="handleSizeChange"
						@current-change="handleCurrentChange"
						layout="total, sizes, prev, pager, next, jumper"
					/>
				</el-card>
			</el-col>
		</el-row>
    <EditUser ref="editUserRef" :title="state.editUserTitle" :orgData="state.orgTreeData" @handleQuery="handleQuery" />
	</div>
  </template>
  
  <script lang="ts" setup name="systemUser">
	import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
	import { ElMessageBox, ElMessage } from 'element-plus';
	import { formatDate } from '/@/utils/formatTime';
	// import { auth } from '/@/utils/authFunction';
	// import OrgTree from '/@/views/system/org/component/orgTree.vue';
	// import EditUser from '/@/views/system/user/component/editUser.vue';

	// import { getAPI } from '/@/utils/axios-utils';
	// import { SysUserApi, SysOrgApi } from '/@/api-services/api';
	// import { SysUser, SysOrg } from '/@/api-services/models';
  import { useUserApi } from '/@/api/user';
  import { useOrgApi } from '/@/api/org';

	// 引入组件
  const OrgTree = defineAsyncComponent(() => import('/@/views/system/org/orgTree.vue'));
  const EditUser = defineAsyncComponent(() => import('/@/views/system/user/dialog.vue'));

	const orgTreeRef = ref<InstanceType<typeof OrgTree>>();
	const editUserRef = ref<InstanceType<typeof EditUser>>();
	const state = reactive({
    userTableData:{
      data: [] as Array<UserForm>,
      queryParams: {
        orgId: 0,
        search: '',
        pageIndex: 1,
        pageSize: 20
      },
      total: 0,
      loading: false
    },
    editUserTitle: '',
		orgTreeData: [] as Array<OrgItem>,
		// userData: [] as Array<SysUser>,
		// queryParams: {
		// 	orgId: -1,
		// 	account: undefined,
		// 	realName: undefined,
		// 	phone: undefined,
		// },
		// tableParams: {
		// 	page: 1,
		// 	pageSize: 20,
		// 	total: 0 as any,
		// },
		// editUserTitle: '',
	});

	onMounted(async () => {
		loadOrgData();
		handleQuery();
	});

	// 查询机构数据
	const loadOrgData = async () => {
		state.loading = true;
    useOrgApi().getOrgList().then(async (res:AjaxResult) => {
      const _data: Array<OrgItem> = res.data;
      state.orgTreeData = _data;
    });
		state.loading = false;
	};

	// 查询操作
	const handleQuery = async () => {
		state.loading = true;

		useUserApi().getUserPage(state.userTableData.queryParams).then(async (res: AjaxResult) => {
		const _data: Array<UserForm> = res.data.items;
		state.userTableData.data = _data;
		state.userTableData.total = res.data?.total;
		});
		setTimeout(() => {
			state.loading = false;
		}, 500);
	};

	// 重置操作
	const resetQuery = () => {
		state.userTableData.queryParams.orgId = 0;
		state.userTableData.queryParams.search = '';
		handleQuery();
	};

	// 打开新增页面
	const openAddUser = (type:string) => {
		state.editUserTitle = '添加账号';
    const _row : UserForm = { id: 0, birthday: '2000-01-01', sex: 1, orderNo: 100, cardType: 0 }
		editUserRef.value?.openDialog(type,_row);
	};

	// 打开编辑页面
	const openEditUser = (type: string, row: UserForm) => {
		state.editUserTitle = '编辑账号';
		editUserRef.value?.openDialog(type, row);
	};

	// 删除
	const delUser = (row: UserForm) => {
		ElMessageBox.confirm(`确定删除账号：【${row.account}】?`, '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning',
		})
		.then(async () => {
			// await useUserApi().delUser() getAPI(SysUserApi).apiSysUserDeletePost({ id: row.id });
			await useUserApi().userDelete({ id: row.id }).then(async (res:AjaxResult) =>{
				handleQuery();
				ElMessage.success('删除成功');
			});
		})
		.catch(() => {});
	};

	// 改变页面容量
	const handleSizeChange = (val: number) => {
		state.userTableData.queryParams.pageSize = val;
		handleQuery();
	};

	// 改变页码序号
	const handleCurrentChange = (val: number) => {
		state.userTableData.queryParams.pageIndex = val;
		handleQuery();
	};

	// 修改状态
	const changeStatus = async (row: any) => {
		await useUserApi().SetStatus({ id: row.id, status: row.status })
		.then(async (res:AjaxResult) => {
			ElMessage.success('账号状态设置成功');
		})
		.catch(() => {
			row.status = row.status == 1 ? 2 : 1;
		});
	};

	// 重置密码
	const resetUserPwd = async (row: any) => {
		ElMessageBox.confirm(`确定重置密码：【${row.account}】?`, '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning',
		})
			.then(async () => {
				await useUserApi().userResetPwd({ id: row.id })
					.then(async (res: AjaxResult) => {
						ElMessage.success(`密码重置成功为：${res.data}`);
					});
			})
			.catch(() => {});
	};

	// 树组件点击
	const nodeClick = async (node: any) => {
		state.userTableData.queryParams.orgId = node.id;
		// state.queryParams.account = undefined;
		// state.queryParams.realName = undefined;
		// state.queryParams.phone = undefined;
		console.log('nId:'+node.id);
		handleQuery();
	};
	</script>

	<style scoped lang="scss">
	.system-user-container {
		:deep(.el-card__body) {
			display: flex;
			flex-direction: column;
			flex: 1;
			overflow: auto;
			.el-table {
				flex: 1;
			}
		}
		.el-pagination
		{
			float: right;
			margin: 10px 0 -10px !important;
		}
	}
	</style>
